
package com.dx.mes.service.query.service;


import static com.dx.mes.common.bean.def.GenericDef.*;
import static com.dx.mes.common.bean.def.GenericDef.E_READ_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.E_RET_PRD_INFO;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef._YES;
import static com.dx.mes.common.bean.def.GenericDef.E_FIPINQSHT_READ_SHT_ERROR;

import java.util.ArrayList;
import java.util.List;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.*;
import com.dx.mes.facade.query.tx.Fipinqsht.*;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.query.service.IFipinqshtService;


@Service("fipinqshtService")
public class FipinqshtService implements IFipinqshtService {
    private LogUtils logUtils;
    private static final String TX_NAME = "FIPINQSHT";

    @Autowired
    private IRetPrdInfoDao retPrdInfoDao;
    @Autowired
    private IRetWODao retWoDao;
    @Autowired
    private IBisOpeDao bisOpeDao;
    @Autowired
    private IBisDataDao bisDataDao;
    @Autowired
    private IBisMdlDefDao bisMdlDefDao;
    @Autowired
    private IBisToolDao bisToolDao;
    @Autowired
    private IBisMdlGrdDao bisMdlGrdDao;
    @Autowired
    private IBisPathiDao bisPathiDao;
    @Autowired
    private IBisToolGrDao bisToolGrDao;
    @Autowired
    private IBisPathDao bisPathDao;

    public String subMainProc(String evt_no, String strInMsg) {
        AppContext.clear();
        AppContext.setCurrServiceName(FipinqshtService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(FipinqshtService.class);
        logUtils.info("[InTrx:" + strInMsg + "]");

        String outMsg = "";
        FipinqshtI inTrx = new FipinqshtI();
        FipinqshtO outTrx = new FipinqshtO();

        outTrx.setTrx_id(TX_NAME);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

        try {
            inTrx = JacksonUtil.fromJson(strInMsg, FipinqshtI.class);

            subMainProc2(inTrx.getAction_flg().toCharArray()[0], inTrx, outTrx);
            if (!outTrx.getRtn_code().equals(RETURN_CODE_OK)) {
                TransactionAspectSupport.currentTransactionStatus()
                        .setRollbackOnly();
            } else {
                retPrdInfoDao.flushCurSession();
            }
        } catch (Exception ex) {
            outTrx.setRtn_mesg(ex.toString());
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            logUtils.error(Util.stackTraceToString(ex));
            TransactionAspectSupport.currentTransactionStatus()
                    .setRollbackOnly();
        } finally {
            outMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + outTrx + "]");
        }
        return outMsg;
    }

    private void subMainProc2(char actionType, FipinqshtI inTrx, FipinqshtO outTrx) {

        switch (actionType) {
            case 'Q': {
                getPrdInfo(inTrx, outTrx);
                break;
            }
            case 'G': {
                getPrdInfos(inTrx, outTrx);
                break;
            }
            case 'C': {
                queryByCondition(inTrx, outTrx);
                break;
            }
            case 'T': {
                getPrdTableRecord(inTrx, outTrx);
                break;
            }
            case 'S': {
                getPrdByTimeAndTool(inTrx, outTrx);
                break;
            }
            case 'I': {
                getPrdWipInfo(inTrx, outTrx);
                break;
            }
            case 'F': {
                getByFabSnProc(inTrx, outTrx);
                break;
            }
            case 'L': {
                getPrdByLotId(inTrx, outTrx);
                break;
            }
            case 'R': {
                checkPrd(inTrx, outTrx);
                break;
            }
            case 'M': {
                getPrdInfoList(inTrx, outTrx);
                break;
            }
            default: {
                String gszMsgBuf = "Invalide type input: [" + actionType + "]";

                outTrx.setRtn_code(String.valueOf(E_FIPINQSHT_INVALID_INPUT));
                outTrx.setRtn_mesg(gszMsgBuf);
                return;
            }
        }
    }

    private void getByFabSnProc(FipinqshtI inTrx, FipinqshtO outTrx) {

        String fabSn = inTrx.getFab_sn();
        String hql = "FROM Ret_prd_info where fab_sn = ?";
        List<Ret_prd_info> retPrdInfoList = retPrdInfoDao.list(hql, fabSn);
        if (retPrdInfoList.size() > 1) {
            outTrx.setHas_more_flg("Y");
        } else {
            outTrx.setHas_more_flg("N");
        }
        List<FipinqshtOA3> oary3List = new ArrayList<FipinqshtOA3>();
        for (Ret_prd_info retPrdInfo : retPrdInfoList) {
            FipinqshtOA3 oary3 = new FipinqshtOA3();
            oary3.setFab_sn(retPrdInfo.getFab_sn());
            oary3.setPrd_seq_id(retPrdInfo.getPrd_seq_id());
            oary3.setWo_id(retPrdInfo.getWo_id_fk());
            oary3List.add(oary3);
        }
        outTrx.setOary3(oary3List);
        outTrx.setOary3_cnt(oary3List.size());
        outTrx.setSht_cnt(oary3List.size());
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

    private void queryByCondition(FipinqshtI inTrx, FipinqshtO outTrx) {

        // Get prd list
        List<Ret_prd_info> ret_prd_infoList = new ArrayList<Ret_prd_info>();
        String sql = "FROM Ret_prd_info WHERE 1=1 ";
        if (!Util.isSpaceCheck(inTrx.getPrd_stat())) {
            sql = sql + " AND prd_stat='" + inTrx.getPrd_stat() + "'";
        } else if (!Util.isSpaceCheck(inTrx.getPrd_stats())) {
            sql = sql + " AND prd_stat IN(" + inTrx.getPrd_stats() + ")";
        }
        if (!Util.isSpaceCheck(inTrx.getOpe_id())) {
            sql = sql + " AND cr_ope_id_fk='" + inTrx.getOpe_id() + "'";
        }
        if (!Util.isSpaceCheck(inTrx.getWo_id())) {
            sql = sql + " AND wo_id_fk='" + inTrx.getWo_id() + "'";
        }
        if (!Util.isSpaceCheck(inTrx.getLot_id())) {
            sql += " AND lot_id = '" + inTrx.getLot_id() + "'";
        }
        if (!Util.isSpaceCheck(inTrx.getSpace_box_id())
                && _YES.equals(inTrx.getSpace_box_id())) {
            sql = sql + " AND box_id_fk=' '";
        } else if (!Util.isSpaceCheck(inTrx.getBox_id())) {
            sql = sql + " AND box_id_fk='" + inTrx.getBox_id() + "'";
        }

        if (!Util.isSpaceCheck(inTrx.getFab_sn())) {
            sql += " AND fab_sn like '%" + inTrx.getFab_sn() + "%'";
        }

        if (!Util.isSpaceCheck(inTrx.getMtrl_box_id())) {
            sql += " AND mtrl_box_id_fk = '" + inTrx.getMtrl_box_id() + "'";
        }
        if (!Util.isSpaceCheck(inTrx.getTool_id())) {
            sql += " AND tool_id_fk = '" + inTrx.getTool_id() + "'";
        }
        ret_prd_infoList = retPrdInfoDao.find(sql);
        if (ret_prd_infoList.isEmpty()) {
            outTrx.setTbl_cnt(0);
        } else {
            outTrx.setTbl_cnt(ret_prd_infoList.size());
            outTrx.setOary_table(ret_prd_infoList);
        }

        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

    private void getPrdWipInfo(FipinqshtI inTrx, FipinqshtO outTrx) {

        String prd_seq_id = inTrx.getPrd_seq_id();
        Ret_prd_info ret_prd_info = retPrdInfoDao.get(prd_seq_id);
        if (ret_prd_info == null) {
            outTrx.setRtn_code(RETURN_CODE_OK);
            outTrx.setRtn_mesg(RETURN_MESG_OK);
            return;
        }
        String prd_stat = ret_prd_info.getPrd_stat();
        FipinqshtOA2 oary2 = new FipinqshtOA2();
        List<FipinqshtOA2> oary2List = new ArrayList<FipinqshtOA2>();
        oary2.setLot_id(ret_prd_info.getLot_id());
        oary2.setPrd_stat(ret_prd_info.getPrd_stat());
        oary2.setWo_id_fk(ret_prd_info.getWo_id_fk());
        if (null != ret_prd_info.getWo_id_fk()) {
            Ret_wo retwo = retWoDao.get(ret_prd_info.getWo_id_fk());
            if (null != retwo) {
                oary2.setMtrl_prod_id_fk(retwo.getMtrl_prod_id_fk());
            }
        }
        oary2.setSo_id_fk(ret_prd_info.getSo_id_fk());
        oary2.setEvt_cate(ret_prd_info.getEvt_cate());
        oary2.setBox_id_fk(ret_prd_info.getBox_id_fk());
        oary2.setSlot_no(ret_prd_info.getSlot_no());
        oary2.setAc_recipe_id(ret_prd_info.getAc_recipe_id());
        oary2.setDs_recipe_id(ret_prd_info.getDs_recipe_id());
        oary2.setPrd_qty(ret_prd_info.getPrd_qty());
        oary2.setTool_port_id_fk(ret_prd_info.getTool_port_id_fk());

        oary2.setFab_sn(ret_prd_info.getFab_sn());

        if (("INPR").equals(prd_stat)) {
            oary2.setOpe_no_fk(ret_prd_info.getCr_ope_no_fk());
            oary2.setOpe_id_fk(ret_prd_info.getCr_ope_id_fk());
            oary2.setOpe_ver_fk(ret_prd_info.getCr_ope_ver_fk());
            oary2.setToolg_id_fk(ret_prd_info.getCr_toolg_id_fk());
            oary2.setPath_id_fk(ret_prd_info.getCr_path_id_fk());
            oary2.setPath_ver_fk(ret_prd_info.getCr_path_ver_fk());
            oary2.setProc_id_fk(ret_prd_info.getCr_proc_id_fk());
        } else {
            oary2.setOpe_no_fk(ret_prd_info.getNx_ope_no_fk());
            oary2.setOpe_id_fk(ret_prd_info.getNx_ope_id_fk());
            oary2.setOpe_ver_fk(ret_prd_info.getNx_ope_ver_fk());
            oary2.setToolg_id_fk(ret_prd_info.getNx_toolg_id_fk());
            oary2.setPath_id_fk(ret_prd_info.getNx_path_id_fk());
            oary2.setPath_ver_fk(ret_prd_info.getNx_path_ver_fk());
            oary2.setProc_id_fk(ret_prd_info.getNx_proc_id_fk());
        }
        // TODO:获取站点信息中文说明
        Bis_opeId opeId = new Bis_opeId();
        opeId.setOpe_id(oary2.getOpe_id_fk());
        opeId.setOpe_ver(oary2.getOpe_ver_fk());
        Bis_ope bis_ope = bisOpeDao.get(opeId);
        if (bis_ope != null) {
            oary2.setOpe_no_dsc(bis_ope.getOpe_dsc());
        }

        // TODO:获取宣告类型的中文说明.
        String sql = "From Bis_data where data_cate='EVCT'and data_id='"
                + oary2.getEvt_cate() + "'";
        List<Bis_data> bis_data_list = bisDataDao.find(sql);
        if (bis_data_list.size() != 0) {
            oary2.setEvt_cate_dsc(bis_data_list.get(0).getData_desc());
        } else {
            oary2.setEvt_cate_dsc(oary2.getEvt_cate());
        }
        // TODO:获取产品规格
        Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_prd_info.getMdl_id_fk());
        if (bis_mdl_def != null) {
            oary2.setMdl_typ_fk(bis_mdl_def.getMdl_dsc());
        }
        oary2.setTool_id_fk(ret_prd_info.getCr_tool_id_fk());
        oary2.setEvt_usr(ret_prd_info.getEvt_usr());
        oary2.setSht_ope_msg(ret_prd_info.getSht_ope_msg());
        oary2.setPrty(ret_prd_info.getPrty());
        oary2.setGroup_id(ret_prd_info.getGroup_id());
        oary2.setPrd_grade(ret_prd_info.getPrd_grade());
        oary2List.add(oary2);
        outTrx.setOary2(oary2List);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

    private void getPrdTableRecord(FipinqshtI inTrx, FipinqshtO outTrx) {

        String prd_seq_id = inTrx.getPrd_seq_id();
        Ret_prd_info ret_prd_info = retPrdInfoDao.get(prd_seq_id);
        outTrx.setPrd_tbl(ret_prd_info);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

    /**
     * Only query box info
     *
     * @return
     */
    private void getPrdInfo(FipinqshtI inTrx, FipinqshtO outTrx) {
        String prd_seq_id = inTrx.getPrd_seq_id();
        String test_cate = inTrx.getTest_cate();
        String query_type = inTrx.getQuery_type();
        String yc_Cnt = "";
        if (StringUtils.isEmpty(prd_seq_id)) {
            String gszMsgBuf = "屏体代码不能为空!";
            outTrx.setRtn_code(String.valueOf(E_READ_NOT_FOUND + E_RET_PRD_INFO));
            outTrx.setRtn_mesg(gszMsgBuf);
            return;
        }
        Ret_prd_info ret_prd_info = retPrdInfoDao.get(prd_seq_id);
        if (null == ret_prd_info) {
            String gszMsgBuf = "屏体代码为[" + prd_seq_id + "]的屏体信息不存在！";
            outTrx.setRtn_code(String.valueOf(E_READ_NOT_FOUND + E_RET_PRD_INFO));
            outTrx.setRtn_mesg(gszMsgBuf);
            return;
        }

        outTrx.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
        outTrx.setPrd_qty(ret_prd_info.getPrd_qty());
        outTrx.setBox_id_fk(ret_prd_info.getBox_id_fk());
        outTrx.setShip_box_id_fk(ret_prd_info.getShip_box_id_fk());
        outTrx.setPrd_stat(ret_prd_info.getPrd_stat());
        outTrx.setPrd_grade(ret_prd_info.getPrd_grade());
        outTrx.setPv_prd_stat_fk(ret_prd_info.getPv_prd_stat_fk());
        outTrx.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
        outTrx.setExt_4(ret_prd_info.getExt_4());
        outTrx.setExt_6(ret_prd_info.getExt_6());
        outTrx.setExt_9(ret_prd_info.getExt_9());
        String dataHql1 = "From Bis_data where data_cate='DFCT' and data_id='" +ret_prd_info.getRsn_code()+"'";
        List<Bis_data> dfct = bisDataDao.find(dataHql1);
        if(dfct.size()!=0 && ret_prd_info.getRsn_code().equals(dfct.get(0).getData_id())){
            outTrx.setRsn_code(dfct.get(0).getData_ext());
        }
        if (!Util.isSpaceCheck(ret_prd_info.getPrd_grade())) {
            String hql = "from Bis_mdl_grd where 1=1 and " +
                    "prd_grade='" + ret_prd_info.getPrd_grade() + "'";
            List<Bis_mdl_grd> bis_mdl_grds = bisMdlGrdDao.find(hql);
            if (bis_mdl_grds.size() != 0) {
                outTrx.setGrade_group(bis_mdl_grds.get(0).getGd_grp());
                outTrx.setOkng_flg(bis_mdl_grds.get(0).getPrd_judge());
            }
        }
        outTrx.setCr_ope_id_fk(ret_prd_info.getCr_ope_id_fk());
        outTrx.setCr_ope_no_fk(ret_prd_info.getCr_ope_no_fk());
        outTrx.setCr_ope_ver_fk(ret_prd_info.getCr_ope_ver_fk());
        outTrx.setCr_path_id_fk(ret_prd_info.getCr_path_id_fk());
        outTrx.setCr_path_ver_fk(ret_prd_info.getCr_path_ver_fk());
        outTrx.setCr_proc_id_fk(ret_prd_info.getCr_proc_id_fk());
        outTrx.setCr_tool_id_fk(ret_prd_info.getCr_tool_id_fk());
        outTrx.setCr_toolg_id_fk(ret_prd_info.getCr_toolg_id_fk());
        
        outTrx.setNx_ope_id_fk(ret_prd_info.getNx_ope_id_fk());
        outTrx.setNx_ope_no_fk(ret_prd_info.getNx_ope_no_fk());
        outTrx.setNx_ope_ver_fk(ret_prd_info.getNx_ope_ver_fk());
        outTrx.setNx_path_id_fk(ret_prd_info.getNx_path_id_fk());
        outTrx.setNx_path_ver_fk(ret_prd_info.getNx_path_ver_fk());
        outTrx.setNx_proc_id_fk(ret_prd_info.getNx_proc_id_fk());
        outTrx.setNx_tool_id_fk(ret_prd_info.getNx_tool_id_fk());
        outTrx.setNx_toolg_id_fk(ret_prd_info.getNx_toolg_id_fk());
        
        outTrx.setWo_id_fk(ret_prd_info.getWo_id_fk());
        if (!Util.isSpaceCheck(ret_prd_info.getWo_id_fk())) {
            Ret_wo ret_wo = retWoDao.get(ret_prd_info.getWo_id_fk());
            if (null != ret_wo) {
                outTrx.setWo_cate(ret_wo.getWo_cate());
            }
        }
        outTrx.setFab_sn(ret_prd_info.getFab_sn());
        outTrx.setSo_id_fk(ret_prd_info.getSo_id_fk());
        outTrx.setLot_id(ret_prd_info.getLot_id());
        outTrx.setSlot_no(ret_prd_info.getSlot_no());
        if (PROC_IN_PROCESS.equals(ret_prd_info.getPrd_stat())) {
            outTrx.setPath_id_fk(ret_prd_info.getCr_path_id_fk());
            outTrx.setPath_ver_fk(ret_prd_info.getCr_path_ver_fk());
            outTrx.setOpe_no_fk(ret_prd_info.getCr_ope_no_fk());
            outTrx.setOpe_id_fk(ret_prd_info.getCr_ope_id_fk());
            outTrx.setOpe_ver_fk(ret_prd_info.getCr_ope_ver_fk());
            outTrx.setProc_id_fk(ret_prd_info.getCr_proc_id_fk());
            outTrx.setToolg_id_fk(ret_prd_info.getCr_toolg_id_fk());
            outTrx.setTool_run_mode(ret_prd_info.getCr_tool_run_mode());
            outTrx.setTool_id_fk(ret_prd_info.getTool_id_fk());
        } else {
            outTrx.setPath_id_fk(ret_prd_info.getNx_path_id_fk());
            outTrx.setPath_ver_fk(ret_prd_info.getNx_path_ver_fk());
            outTrx.setOpe_no_fk(ret_prd_info.getNx_ope_no_fk());
            outTrx.setOpe_id_fk(ret_prd_info.getNx_ope_id_fk());
            outTrx.setOpe_ver_fk(ret_prd_info.getNx_ope_ver_fk());
            outTrx.setProc_id_fk(ret_prd_info.getNx_proc_id_fk());
            outTrx.setToolg_id_fk(ret_prd_info.getNx_toolg_id_fk());
            outTrx.setTool_run_mode(ret_prd_info.getNx_tool_run_mode());
            outTrx.setTool_id_fk(ret_prd_info.getNx_tool_id_fk());
        }
        String dataHql = "From Bis_data where data_cate='OPCT' and data_ext='" + outTrx.getOpe_id_fk()+"'";
        List<Bis_data> opcts = bisDataDao.find(dataHql);
        if(opcts.size()==0){
            outTrx.setOpe_cate("P");
        }else{
            outTrx.setOpe_cate("M");
        }
        Bis_opeId bis_opeId = new Bis_opeId();
        bis_opeId.setOpe_id(outTrx.getOpe_id_fk());
        bis_opeId.setOpe_ver(outTrx.getOpe_ver_fk());
        Bis_ope bis_ope = bisOpeDao.get(bis_opeId);
        if (null != bis_ope) {
            String hqltool = "FROM Bis_tool_gr where 1=1 and toolg_id='" + bis_ope.getToolg_id_fk() + "'";
            List<Bis_tool_gr> bisToolGr = bisToolGrDao.find(hqltool);
            if (bisToolGr.size() != 0) {
                outTrx.setToolByope(bisToolGr.get(0).getId().getTool_id_fk());
            }
            outTrx.setOpe_dsc(bis_ope.getOpe_dsc());
        }
        Bis_pathiId bis_pathiId = new Bis_pathiId();
        bis_pathiId.setPath_id(outTrx.getPath_id_fk());
        bis_pathiId.setPath_ver(outTrx.getPath_ver_fk());
        Bis_pathi bis_pathi = bisPathiDao.get(bis_pathiId);
        if(bis_pathi != null){
            outTrx.setPath_cate(bis_pathi.getPath_cate());
        }

        outTrx.setPrd_stat(ret_prd_info.getPrd_stat());

        if (ret_prd_info.getLogon_timestamp() != null) {
            outTrx.setLogon_timestamp(ret_prd_info.getLogon_timestamp().toString());
        }
        if (ret_prd_info.getEvt_timestamp() != null) {
            outTrx.setEvt_timestamp(ret_prd_info.getEvt_timestamp().toString());
        }
        if (!Util.isSpaceCheck(ret_prd_info.getMdl_id_fk())) {
            Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_prd_info.getMdl_id_fk());
            if (null != bis_mdl_def) {
                outTrx.setCus_id(bis_mdl_def.getCus_id());
            }
            String hql = "FROM Bis_data where data_cate ='BTCT' and data_ext='" + ret_prd_info.getMdl_id_fk() + "'";
            List<Bis_data> bis_dataList = bisDataDao.find(hql);
            if (bis_dataList.size() != 0) {
                outTrx.setBatch_cnt(bis_dataList.get(0).getData_item());
                outTrx.setSmp_cnt(bis_dataList.get(0).getExt_1());
                yc_Cnt = bis_dataList.get(0).getExt_2();
            }
        }
        if (!Util.isSpaceCheck(ret_prd_info.getCr_tool_id_fk())) {
            Bis_tool bis_tool = bisToolDao.get(ret_prd_info.getCr_tool_id_fk());
            if (null != bis_tool) {
                outTrx.setFab_site_id_fk(bis_tool.getFab_site_fk());
                outTrx.setFab_id_fk(bis_tool.getFab_id_fk());
                outTrx.setBay_id(bis_tool.getBay_id());
            }
        }
        if(!Util.isSpaceCheck(ret_prd_info.getLot_id())){
            String hqlLotCnt = "FROM Ret_prd_info where lot_id='" + ret_prd_info.getLot_id() +"'";
            List<Ret_prd_info> prdLots = retPrdInfoDao.find(hqlLotCnt);
            outTrx.setLot_cnt(Integer.valueOf(prdLots.size()).toString());
        }
        if(!Util.isSpaceCheck(test_cate)){
            String hqlCnt = "FROM Ret_prd_info where lot_id='" + ret_prd_info.getLot_id() +"'";
            if(test_cate.equals("T")){
                hqlCnt = hqlCnt + " and ext_1 is not null";
            }else if(test_cate.equals("D")){
                hqlCnt = hqlCnt + " and ext_3 is not null";
            }else if(test_cate.equals("W")){
                hqlCnt = hqlCnt + " and ext_5 is not null";
            }
            List<Ret_prd_info> prds = retPrdInfoDao.find(hqlCnt);
            outTrx.setTest_cnt(Integer.valueOf(prds.size()).toString());
            String hqlabn = "FROM Ret_prd_info where lot_id='" + ret_prd_info.getLot_id() +"'";
            if(test_cate.equals("T")){
                hqlabn = hqlabn + " and ext_1 is not null and ext_2='NG'";
            }else if(test_cate.equals("D")){
                hqlabn = hqlabn + " and ext_3 is not null and ext_4='NG'";
            }else if(test_cate.equals("W")){
                hqlabn = hqlabn + " and ext_5 is not null and ext_6='NG'";
            }
            List<Ret_prd_info> prdabns = retPrdInfoDao.find(hqlabn);
            outTrx.setAbn_cnt(Integer.valueOf(prdabns.size()).toString());

        }
        if(!Util.isSpaceCheck(query_type)&&"O".equals(query_type)){
            //tp
            String tpTest = "FROM Ret_prd_info where lot_id='" + ret_prd_info.getLot_id() +"' and ext_1 is not null";
            List<Ret_prd_info> tpTestPrds = retPrdInfoDao.find(tpTest);
            outTrx.setTp_test_cnt(Integer.valueOf(tpTestPrds.size()).toString());

            String tpAbn = "FROM Ret_prd_info where lot_id='" + ret_prd_info.getLot_id() +"' and ext_1 is not null " +
                    "and ext_2='NG'";
            List<Ret_prd_info> tpAbnPrds = retPrdInfoDao.find(tpAbn);
            outTrx.setTp_abn_cnt(Integer.valueOf(tpAbnPrds.size()).toString());

            //dx
            String dxTest = "FROM Ret_prd_info where lot_id='" + ret_prd_info.getLot_id() +"' and ext_3 is not null";
            List<Ret_prd_info> dxTestPrds = retPrdInfoDao.find(dxTest);
            outTrx.setDx_test_cnt(Integer.valueOf(dxTestPrds.size()).toString());

            String dxAbn = "FROM Ret_prd_info where lot_id='" + ret_prd_info.getLot_id() +"' and ext_3 is not null " +
                    "and ext_4='NG'";
            List<Ret_prd_info> dxAbnPrds = retPrdInfoDao.find(dxAbn);
            outTrx.setDx_abn_cnt(Integer.valueOf(dxAbnPrds.size()).toString());

            //wg
            String wgTest = "FROM Ret_prd_info where lot_id='" + ret_prd_info.getLot_id() +"' and ext_5 is not null";
            List<Ret_prd_info> wgTestPrds = retPrdInfoDao.find(wgTest);
            outTrx.setWg_test_cnt(Integer.valueOf(wgTestPrds.size()).toString());

            String wgAbn = "FROM Ret_prd_info where lot_id='" + ret_prd_info.getLot_id() +"' and ext_5 is not null " +
                    "and ext_6='NG'";
            List<Ret_prd_info> wgAbnPrds = retPrdInfoDao.find(wgAbn);
            outTrx.setWg_abn_cnt(Integer.valueOf(wgAbnPrds.size()).toString());


        }
        //返回批次信息
        String hqlM="FROM Ret_prd_info where lot_id='" + ret_prd_info.getLot_id() +"'";
        List<Ret_prd_info> prdLotList = retPrdInfoDao.find(hqlM);
        //根据当前批次筛选出是否只能批退
        Integer ycCnt = 0;
        if(!StringUtils.isEmpty(yc_Cnt)){
            ycCnt = Integer.valueOf(yc_Cnt);
        }
        Integer prdYcCnt = 0;
        for(int i = 0 ;i < prdLotList.size(); i++){
            if("NG".equals(prdLotList.get(i).getPrd_grade())){
                prdYcCnt++;
            }
        }
        if(prdYcCnt > ycCnt){
            outTrx.setShowflag(true);
        }else{
            outTrx.setShowflag(false);
        }

        outTrx.setOary_table(prdLotList);
        outTrx.setEvt_usr(ret_prd_info.getEvt_usr());
        outTrx.setSht_ope_msg(ret_prd_info.getSht_ope_msg());

        //获取报废状态
        if(PROC_WAIT.equals(ret_prd_info.getPrd_stat()) && Util.isSpaceCheck(ret_prd_info.getBnk_flg())){
            outTrx.setScrp_stat("待报废");
        }
        if(PROC_SCRP.equals(ret_prd_info.getPrd_stat()) && BANK_IN.equals(ret_prd_info.getBnk_flg())){
            outTrx.setScrp_stat("报废中");
        }
        if(PROC_SCRP.equals(ret_prd_info.getPrd_stat()) && BANK_COMP.equals(ret_prd_info.getBnk_flg())){
            outTrx.setScrp_stat("已报废");
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

    private void getPrdInfos(FipinqshtI inTrx, FipinqshtO outTrx) {
        String prd_seq_id = inTrx.getPrd_seq_id();
        String[] prdids = prd_seq_id.split("\n");
        ArrayList<FipinqshtOS> os = new ArrayList<>();
        for (String prdid : prdids) {
            Ret_prd_info ret_prd_info = retPrdInfoDao.get(prdid);
            if (ret_prd_info != null) {
                FipinqshtOS fipinqshtOS = new FipinqshtOS();
                fipinqshtOS.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
                fipinqshtOS.setPrd_qty(ret_prd_info.getPrd_qty());
                fipinqshtOS.setBox_id_fk(ret_prd_info.getBox_id_fk());
                fipinqshtOS.setPrd_stat(ret_prd_info.getPrd_stat());
                fipinqshtOS.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
                fipinqshtOS.setCr_ope_id_fk(ret_prd_info.getCr_ope_id_fk());
                fipinqshtOS.setCr_ope_no_fk(ret_prd_info.getCr_ope_no_fk());
                fipinqshtOS.setCr_ope_ver_fk(ret_prd_info.getCr_ope_ver_fk());
                fipinqshtOS.setCr_path_id_fk(ret_prd_info.getCr_path_id_fk());
                fipinqshtOS.setCr_path_ver_fk(ret_prd_info.getCr_path_ver_fk());
                fipinqshtOS.setCr_proc_id_fk(ret_prd_info.getCr_proc_id_fk());
                fipinqshtOS.setCr_tool_id_fk(ret_prd_info.getCr_tool_id_fk());
                fipinqshtOS.setWo_id_fk(ret_prd_info.getWo_id_fk());
                fipinqshtOS.setFab_sn(ret_prd_info.getFab_sn());
                fipinqshtOS.setSo_id_fk(ret_prd_info.getSo_id_fk());

                os.add(fipinqshtOS);
                outTrx.setOarys(os);
            } else {
                outTrx.setRtn_code(RETURN_CODE_SET_ERROR);
                outTrx.setRtn_mesg("" + prdid + "此产品不存在");
                return;
            }

        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

    private void getPrdByTimeAndTool(FipinqshtI inTrx, FipinqshtO outTrx) {

        String fileLastModifyTimestamp = inTrx.getStrTimeStamp();
        StringBuffer buffer = new StringBuffer();
        String toolID = inTrx.getTool_id();
        List<Ret_prd_info> retPrdInfoList = new ArrayList<Ret_prd_info>();
        List<FipinqshtOA> oaryList = new ArrayList<FipinqshtOA>();
        Session session = null;
        try {
            String sql = "SELECT * FROM RET_PRD_INFO where evt_timestamp between"
                    + " DATE_ADD('"
                    + fileLastModifyTimestamp
                    + "',INTERVAL -3 SECOND) AND"
                    + " DATE_ADD('"
                    + fileLastModifyTimestamp
                    + "',INTERVAL 3 SECOND) AND"
                    + " cr_tool_id_fk = '" + toolID + "'";
            buffer.append("SELECT * FROM RET_PRD_INFO where ")
                    .append(" prd_stat = 'INPR'")
                    .append(" AND cr_tool_id_fk='").append(toolID).append("'")
                    .append(" AND evt_timestamp between").append(" DATE_ADD('")
                    .append(fileLastModifyTimestamp)
                    .append("',INTERVAL -300 SECOND)").append(" AND '")
                    .append(fileLastModifyTimestamp).append("'")
                    .append(" ORDER BY evt_timestamp desc");
            retPrdInfoList = retPrdInfoDao.findBySQL(buffer.toString(),
                    Ret_prd_info.class);
        } catch (Exception ex) {
            outTrx.setRtn_code(String.valueOf(E_FIPINQSHT_READ_SHT_ERROR));
            outTrx.setRtn_mesg(ex.toString());

            logUtils.error("read RET_PRD_INFO ERROR:" + ex.toString());
            logUtils.error(Util.stackTraceToString(ex));
        }

        for (Ret_prd_info ret_prd_info : retPrdInfoList) {
            FipinqshtOA oary = new FipinqshtOA();
            oary.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
            oary.setFab_sn(ret_prd_info.getFab_sn());
            oary.setCr_ope_id(ret_prd_info.getCr_ope_id_fk());
            oary.setCr_ope_no(ret_prd_info.getCr_ope_no_fk());
            oary.setCr_ope_ver(ret_prd_info.getCr_ope_ver_fk());
            oary.setCr_path_id(ret_prd_info.getCr_path_id_fk());
            oary.setCr_path_ver(ret_prd_info.getCr_path_ver_fk());
            oary.setCr_proc_id_fk(ret_prd_info.getCr_proc_id_fk());
            oaryList.add(oary);
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        outTrx.setOary(oaryList);
        outTrx.setSht_cnt(oaryList.size());

    }

    private void getPrdByLotId(FipinqshtI inTrx, FipinqshtO outTrx) {

        String prd_seq_id = inTrx.getPrd_seq_id();
        String lotId = inTrx.getLot_id();
        String lotCnt = "";
        if(Util.isSpaceCheck(lotId)){
            Ret_prd_info ret_prd_info = retPrdInfoDao.get(prd_seq_id);
            if (null == ret_prd_info) {
                outTrx.setRtn_code(RETURN_CODE_SET_ERROR);
                outTrx.setRtn_mesg("" + prd_seq_id + "此产品不存在");
                return;
            }
            lotId = ret_prd_info.getLot_id();
        }

        String hql = "FROM Ret_prd_info where lot_id = ?";
        List<Ret_prd_info> retPrdInfoList = retPrdInfoDao.list(hql, lotId);
        List<FipinqshtOA3> oary3List = new ArrayList<FipinqshtOA3>();
        if(retPrdInfoList.size()!=0){
            String hqlLot = "FROM Bis_data where data_cate ='BTCT' and data_ext='" + retPrdInfoList.get(0).getMdl_id_fk() + "'";
            List<Bis_data> bis_dataList = bisDataDao.find(hqlLot);
            if (bis_dataList.size() != 0) {
                lotCnt = bis_dataList.get(0).getData_item();
            }
        }

        for (Ret_prd_info retPrdInfo : retPrdInfoList) {
            FipinqshtOA3 oary3 = new FipinqshtOA3();
            oary3.setPrd_seq_id(retPrdInfo.getPrd_seq_id());
            oary3.setWo_id(retPrdInfo.getWo_id_fk());
            oary3.setPrd_stat(retPrdInfo.getPrd_stat());
            oary3.setMdl_id_fk(retPrdInfo.getMdl_id_fk());
            if (PROC_IN_PROCESS.equals(retPrdInfo.getPrd_stat())) {
                oary3.setOpe_id(retPrdInfo.getCr_ope_id_fk());
            } else if (PROC_WAIT.equals(retPrdInfo.getPrd_stat())) {
                oary3.setOpe_id(retPrdInfo.getNx_ope_id_fk());
            }
            oary3.setLot_id(retPrdInfo.getLot_id());
            oary3.setPrd_grade(retPrdInfo.getPrd_grade());
            oary3.setLot_cnt(lotCnt);
            oary3List.add(oary3);
        }

        outTrx.setOary3(oary3List);
        outTrx.setOary3_cnt(oary3List.size());
        outTrx.setSht_cnt(oary3List.size());
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

    private void checkPrd(FipinqshtI inTrx, FipinqshtO outTrx) {

        String woId = inTrx.getWo_id();
        String opeId = inTrx.getOpe_id();
        String prdSeqId = inTrx.getPrd_seq_id();
        String skip_flg = "";
        Ret_prd_info retPrdInfo = retPrdInfoDao.get(prdSeqId);
        if (null == retPrdInfo) {
            outTrx.setRtn_mesg("屏体ID为[" + inTrx.getPrd_seq_id() + "]屏体不存在！");
            outTrx.setRtn_code(String.valueOf(E_RET_PRD_INFO + E_READ_NOT_FOUND));
            return;
        }
        if(!opeId.equals(retPrdInfo.getNx_ope_id_fk())){
            outTrx.setRtn_mesg("屏体站点为[" + retPrdInfo.getNx_ope_id_fk() + "]，请去该站点入账！");
            outTrx.setRtn_code(String.valueOf(E_RET_PRD_INFO + E_READ_NOT_FOUND));
            return;
        }
        if (retPrdInfo.getPrd_stat().equals("INPR")) {
            String hql = "FROM Bis_path where path_id_fk='" + retPrdInfo.getNx_path_id_fk() + "' and path_ver_fk='" +
                    retPrdInfo.getNx_path_ver_fk() + "' and cr_ope_id_fk='" + retPrdInfo.getCr_ope_id_fk() + "'";
            List<Bis_path> paths = bisPathDao.find(hql);
            if (paths.size() != 0) {
                if (!Util.isSpaceCheck(paths.get(0).getSkip_flg())) {
                    skip_flg = paths.get(0).getSkip_flg();
                }
            }
        }
        outTrx.setSkip_flg(skip_flg);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
    private void getPrdInfoList(FipinqshtI inTrx, FipinqshtO outTrx) {

        String woId = inTrx.getWo_id();
        String opeId = inTrx.getOpe_id();
        String prdStat= inTrx.getPrd_stat();
        String prdSeqId = inTrx.getPrd_seq_id();
        String hql = "FROM Ret_prd_info where 1=1";
        if (!Util.isSpaceCheck(woId)) {
            hql=hql+" and wo_id_fk ='" +woId+"'";
        }
        if (!Util.isSpaceCheck(opeId)) {
            hql=hql+" and nx_ope_id_fk ='" +opeId+"'";
        }
        if (!Util.isSpaceCheck(prdStat)) {
            hql=hql+" and prd_stat ='" +prdStat+"'";
        }
        if (!Util.isSpaceCheck(prdSeqId)) {
            hql=hql+" and prd_seq_id ='" +prdSeqId+"'";
        }
         List<Ret_prd_info> ret_prd_infos = retPrdInfoDao.find(hql);
         List<FipinqshtOA2> oaryAList = new ArrayList<>();
        for(Ret_prd_info ret_prd_info :ret_prd_infos ){
            FipinqshtOA2 oary = new FipinqshtOA2();
            oary.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
            oary.setWo_id_fk(ret_prd_info.getWo_id_fk());
            oary.setPrd_grade(ret_prd_info.getPrd_grade());
            oary.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
            oary.setPath_id_fk(ret_prd_info.getNx_path_id_fk());
            oary.setPath_ver_fk(ret_prd_info.getNx_path_ver_fk());
            oary.setNx_ope_id_fk(ret_prd_info.getNx_ope_id_fk());
            oary.setPrd_stat(ret_prd_info.getPrd_stat());
            oaryAList.add(oary);
        }
        outTrx.setOary2(oaryAList);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
}
